﻿using System;
using System.Linq;
using System.Web.Mvc;
using TrainingManagerSystem.WebUI.Models;
using TrainingManagerSystem.WebUI.TrainingManagerSystemService;

namespace TrainingManagerSystem.WebUI.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAccountManager _accountManager;
        private readonly TrainingManagerSystemModelClient _service = new TrainingManagerSystemModelClient();
        private string AccountId = Environment.UserName;

        public AccountController() {
            _accountManager = new AccountManager();
        }

        //
        // GET: /Account/
        public ActionResult Index() {
            var accountViewModel = new AccountViewModel {
                                                            Accounts = _accountManager.Search(null, null, null, null, null, 0, null, null, 0, 0, 1, 20),
                                                            Locations = new SelectList(_accountManager.FetchAllLocation(), "LocationId", "LocationName"),
                                                            ContractTypes = new SelectList(_accountManager.FetchAllContractType(), "ContractTypeId", "ContractTypeName"),
                                                            JobTitles = new SelectList(_accountManager.FetchAllJobTitle(), "JobTitleId", "JobTitleName"),
                                                            Statuses = new SelectList(_accountManager.FetchAllStatusAccount(), "StatusAccountId", "StatusAccountName"),
                                                            UserDepartment = 0 //departmentId of user's logged in
                                                        };
            
            var host = ManagerClientService.Client();
            ViewBag.Data = AcountCourse.GetDataForTreeView(host.ListAllDepartment(0).ToList());

            return View(accountViewModel);
        }

        public JsonResult SearchAjax(string accountName, string fullName,
            int? locationId, int? jobTitleId, string employeeId, int departmentId,
            int? contractTypeId, int? statusAccountId, int? minMonth, int? maxMonth,
            int pageNumber = 1, int pageSize = 10) {
            return Json(_accountManager.Search(accountName, fullName, locationId,
                jobTitleId, employeeId, departmentId, contractTypeId, statusAccountId, 
                minMonth ?? 0, maxMonth ?? 0, pageNumber, pageSize));
        }

        public JsonResult GetAccountDetails(string employeeId) {
            return Json(_accountManager.GetAccountByEmployeeId(employeeId));
        }

        public JsonResult TreeSearchAjax(int node) {
            return null;
        }

        //QuyenNTH

        public PartialViewResult AccountCourseRefund() {
            if (_service.CheckRole(AccountId, PermissionViewConst.Course))
            {
                return PartialView("_PersonCourse", _accountManager.AccountCourseRefund("00000007"));
            }
            return PartialView("Empty");
        }

        public PartialViewResult AccountCertificateRefund() {
            if (_service.CheckRole(AccountId, PermissionViewConst.AccountCertificate))
            {
                return PartialView("_PersonCertificate", _accountManager.AccountCertificateRefund("00000007"));
            }
            return PartialView("Empty");
        }

        //End QuyenNTH
    }
}